package com.hblack.manage.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.hblack.manage.pojo.ItemCat;
import com.hblack.manage.service.ItemCatService;

/**
 * 根据parentId查询商品类目
 * @author Hblack
 *
 */
@Controller
@RequestMapping(value="item/cat")
public class ItemCatController {
	
	@Autowired
	private ItemCatService itemCatService;
	
	@RequestMapping(method = RequestMethod.GET)
	public ResponseEntity<List<ItemCat>> queryItemCatByParentId(@RequestParam(value="id" ,defaultValue="0") Long parentId){
		
		try {
			//改用BaseService中方法查询
			//List<ItemCat> list = itemCatService.queryItemCatsByParentId(parentId);
			
			ItemCat itemCat = new ItemCat();
			itemCat.setParentId(parentId);
			//只查询商品类目状态为1即没有被删除
			itemCat.setStatus(1);
			
			//根据条件查询
			List<ItemCat> list = itemCatService.queryListByWhere(itemCat);
			
			if(list == null || list.size() == 0) {
				//return ResponseEntity.status(HttpStatus.OK).body(list);
				return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);		//没有查询到数据，返回404状态码
			}else {
				return ResponseEntity.ok(list);										//查询到数据则返回状态码200，以及数据
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);	//抛出异常则返回500
	}
	
	@RequestMapping(value="{cid}" ,method=RequestMethod.GET)
	public ResponseEntity<ItemCat> queryItemCatById(@PathVariable("cid") Long cid){
		
		try {
			ItemCat itemCat = itemCatService.queryById(cid);
			if(itemCat == null){
				return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
			}else{
				return ResponseEntity.ok(itemCat);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}
}
